package com.yonyou.marketing.app.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.AppResponse;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.marketing.app.api.constant.AppConstant;
import com.yonyou.marketing.app.api.dto.AppMaintainDto;
import com.yonyou.marketing.app.api.service.AppMaintainService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator TODO description
 * @date 2017年11月16日
 */
@RestController
@RequestMapping(value = AppConstant.BASE_PATH + "/" + AppConstant.VERSION)
@Api("养修预约")
public class AppMaintainController {
	private Logger          logger = Logger.getLogger(AppLoginController.class);

    @Autowired
    private AppMaintainService appMaintainService;
    
    /**
     * 按距离查找销售店
     * 
     * @author Administrator
     * @date 2017年11月16日
     * @return
     */
    @RequestMapping(value = "/searchInit", method = RequestMethod.POST)
    @ApiOperation(value = "查询销售店", notes = "查询销售店")
    public AppResponse<Map<String, Object>> searchDealer(
    		 @RequestParam(required = false, defaultValue = "") String pageSize
    		,@RequestParam(required = false, defaultValue = "") String pageNo
    		,@RequestParam(required = false, defaultValue = "") String gz //关注查询参数
    		,@RequestParam(required = false, defaultValue = "") String lat //推荐店查询参数
    		,@RequestParam(required = false, defaultValue = "") String lng //推荐店查询参数
    		,@RequestParam(required = false, defaultValue = "") String fordWord //关键字查询参数
    		,@RequestParam(required = false, defaultValue = "") String regionCode //区域查询参数
    		,@RequestParam(required = false, defaultValue = "") Integer userId) {
        logger.info("searchDealer==>start...");
        try {
        	Map<String,String> pa = new HashMap<>();
        	pa.put("pageSize", pageSize);
        	pa.put("pageNo", pageNo);
        	pa.put("gz", gz);
        	pa.put("lat", lat);
        	pa.put("lng", lng);
        	pa.put("fordWord", "%"+fordWord+"%");
        	pa.put("regionCode", regionCode);
        	logger.info("searchDealer==>userId:"+userId);
        	logger.info("searchDealer==>pa:"+pa);
        	Map<String,Object> result = appMaintainService.selectDealer(pa,userId);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("searchDealer==>end...");
        }
    }

    /**
     * 查找销售店关注店
     * 
     * @author Administrator
     * @date 2017年11月16日
     * @return
     */
    @RequestMapping(value = "/upGzDealerCode", method = RequestMethod.POST)
    @ApiOperation(value = "保存关注销售店", notes = "保存关注销售店")
    public AppResponse<Map<String, Object>> upGzDealerCode(@RequestParam(required = false, defaultValue = "") String dealerCode
    		,@RequestParam(required = false, defaultValue = "") Integer userId) {
        logger.info("upGzDealerCode==>start...");
        try {
        	logger.info("upGzDealerCode==>dealerCode:"+dealerCode +"  userId:"+userId);
        	Map<String,Object> result = appMaintainService.upGzDealerCode( dealerCode,userId);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("upGzDealerCode==>end...");
        }
    }
    /**
     *  获取区域信息
     * 
     * @author Administrator
     * @date 2017年11月16日
     * @return
     */
    @RequestMapping(value = "/getArea", method = RequestMethod.POST)
    @ApiOperation(value = "获取城市", notes = "获取城市")
    public AppResponse<Map<String, Object>> getCity() {
        logger.info("getCity==>start...");
        try {
        	Map<String,Object> result = new HashMap<>();
        	List<Map<String, Object>> list = appMaintainService.getCity();
        	result.put("rows", list);
        	result.put("total", list.size());
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("getCity==>end...");
        }
    }

    /**
     *  获取销售顾问列表
     * 
     * @author Administrator
     * @date 2017年11月17日
     * @return
     */
    @RequestMapping(value = "/getAdviserList", method = RequestMethod.POST)
    @ApiOperation(value = "获取销售顾问", notes = "获取销售顾问")
    public AppResponse<Map<String, Object>> getCounselorData(@RequestParam("dealerCode") String dealerCode,
            @RequestParam("positionId") String positionId) {
        logger.info("getCounselorData==>start...");
        try {
        	Map<String,Object> result = new HashMap<>();
        	List<Map<String, Object>> list = appMaintainService.getCounselorData(dealerCode,positionId);
        	result.put("rows", list);
        	result.put("total", list.size());
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("getCounselorData==>end...");
        }
    }
    /**
     *  获取车辆信息
     * 
     * @author Administrator
     * @date 2017年11月17日
     * @return
     */
    @RequestMapping(value = "/loadCarMaintaininfoList", method = RequestMethod.POST)
    @ApiOperation(value = "获取车辆信息", notes = "获取车辆信息")
    public AppResponse<Map<String, Object>> getMaintainSel(@RequestParam("userId") String userId) {
        logger.info("getMaintainSel==>start...");
        try {
        	Map<String,Object> result = new HashMap<>();
        	List<AppMaintainDto> list = appMaintainService.getMaintainSel(userId);
        	result.put("carList", list);
        	result.put("total", list.size());
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("getMaintainSel==>end...");
        }
    }
    /**
     *  选择到店时间
     * 
     * @author Administrator
     * @date 2017年11月20日
     * @return
     */
    @RequestMapping(value = "/getMrFreeTime", method = RequestMethod.POST)
    @ApiOperation(value = "获取工位空闲时间", notes = "获取工位空闲时间")
    public AppResponse<Map<String, Object>> getMrFreeTime(@RequestParam("dealerId") String dealerId) {
    	try {
        	Map<String,Object> result = new HashMap<>();
        	List<Map<String, Object>> list = appMaintainService.getMrFreeTime(dealerId);
        	result.put("rows", list);
        	result.put("total", list.size());
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }
    
    /**
     *  选择预约类型
     * 
     * @author Administrator
     * @date 2017年11月20日
     * @return
     */
    @RequestMapping(value = "/getMaintainType", method = RequestMethod.POST)
    @ApiOperation(value = "选择预约类型", notes = "选择预约类型")
    public AppResponse<Map<String, Object>> getMaintainType() {
    	try {
        	Map<String,Object> result = new HashMap<>();
        	List<Map<String, Object>> list = appMaintainService.getMaintainType();
        	result.put("rows", list);
        	result.put("total", list.size());
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }
    /**
     *  保存预约信息
     * 
     * @author Administrator
     * @date 2017年11月21日
     * @return
     */
    @RequestMapping(value = "/doAppoint", method = RequestMethod.POST)
    @ApiOperation(value = "保存预约信息", notes = "保存预约信息")
    public AppResponse<Map<String, Object>> saveMaintain(@RequestParam Map<String,Object> param) {
    	try {
        	Map<String,Object> result = appMaintainService.saveMaintain(param);
        	logger.info("saveMaintain---"+result);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("保存发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }

    
    /**
     *  养修预约列表查询
     * 
     * @author Administrator
     * @date 2017年11月22日
     * @return
     */
    @RequestMapping(value = "/checkAppointMessage", method = RequestMethod.POST)
    @ApiOperation(value = "养修预约列表查询", notes = "养修预约列表查询")
    public AppResponse<Map<String, Object>> getMaintainData(@RequestParam("userId") String userId) {
    	try {
    		Map<String,Object> result = new HashMap<>();
    		List<AppMaintainDto> list = appMaintainService.getMaintainData(userId);
        	result.put("rows", list);
        	result.put("total", list.size());
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }
    
    /**
     *  预约内容查询（目前写死，数据库没有这张表）
     * 
     * @author Administrator
     * @date 2017年11月23日
     * @return
     */
    @RequestMapping(value = "/getResData", method = RequestMethod.POST)
    @ApiOperation(value = "预约内容查询", notes = "预约内容查询")
    public AppResponse<Map<String, Object>> getResData() {
    	try {
    		Map<String,Object> result = new HashMap<>();
    		List<String> name = new ArrayList<>();
    		List<String> fault = new ArrayList<>();
    		name.add("大保养");
    		name.add("首保");
    		name.add("小保养");
    		name.add("其他");
    		fault.add("怠速抖动");
    		fault.add("排气异常");
    		fault.add("换挡冲击");
    		fault.add("变速器脱档");
    		fault.add("传动系统震动");
    		fault.add("跑偏/轮胎异常磨损");
    		fault.add("启动困难");
    		fault.add("故障灯亮");
    		fault.add("其他");
        	result.put("content", name);
        	result.put("fault", fault);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }
    /**
     * 查询预约详情
     * @author Administrator
     * @date 2017年11月23日
     * @return 
     */
    @RequestMapping(value = "/detailApplyInfo", method = RequestMethod.POST)
    @ApiOperation(value = "查询预约详情", notes = "查询预约详情")
    public AppResponse<Map<String, Object>> getMaintainDataById(@RequestParam("resnId") String resnId) {
    	try {
    		Map<String,Object> result = new HashMap<>();
        	result.put("rows", appMaintainService.getMaintainDataById(resnId));
        	result.put("total", 1);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }
    /**
     * 取消订单
     * @author Administrator
     * @date 2017年11月23日
     * @return 
     */
    @RequestMapping(value = "/cancelAppoint", method = RequestMethod.POST)
    @ApiOperation(value = "取消订单", notes = "取消订单")
    public AppResponse<Map<String, Object>> cancelAppoint(@RequestParam("applyNum") String applyNum) {
    	try {
    		Map<String,Object> result = appMaintainService.cancelAppoint(applyNum);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result ,"");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
        } 
    }
}
